package devcamp.teama.yahooooooo.api;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.GeoPt;

import devcamp.teama.yahooooooo.dao.YahoooooooDao;
import devcamp.teama.yahooooooo.dto.LocationDto;
import devcamp.teama.yahooooooo.dto.RegistrationDto;
import devcamp.teama.yahooooooo.json.YahoooooooJson;
import devcamp.teama.yahooooooo.util.ParameterUtils;
import devcamp.teama.yahooooooo.util.YahoooooooUtils;

public class YahoooooooServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        yahooooooo(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        yahooooooo(req, resp);
    }

    void yahooooooo(HttpServletRequest req, HttpServletResponse resp) {
        YahoooooooJson json = new YahoooooooJson();
        Long id = null;
        Float latitude = null;
        Float longitude = null;
        // パラメータを取得して
        String strId = req.getParameter(ParameterUtils.ID);
        String strLatitude = req.getParameter(ParameterUtils.LATITUDE);
        String strLongitude = req.getParameter(ParameterUtils.LONGITUDE);
        String location = req.getParameter(ParameterUtils.LOCATION);

        if (!validate(strId, strLatitude, strLongitude, location)) {
            json.registration = false;
            printJson(resp, YahoooooooUtils.Object2Json(json));
            return;
        }

        // 数値変換
        try {
            id = YahoooooooUtils.String2Long(strId);
            latitude = YahoooooooUtils.String2Float(strLatitude);
            longitude = YahoooooooUtils.String2Float(strLongitude);
        } catch (NumberFormatException e) {
            //FIXME:エラーログの出力。エラーログにその時の文字列を入れておく
            json.registration = false;
            printJson(resp, YahoooooooUtils.Object2Json(json));
            return;
        }
        // ユーザーを特定
        YahoooooooDao dao = new YahoooooooDao();
        RegistrationDto userData = dao.findUserById(id);

        // データストアに保存
        new GeoPt(latitude, longitude);
        LocationDto dto = new LocationDto();
        dto.setId(id);
        dto.setLatitude(latitude);
        dto.setLongitude(longitude);
        dto.setLocation(location);
        dto.setTwitterId(userData.getTwitterId());
        dto.setUserName(userData.getUserName());

        // 結果を設定
        dao.insert(dto);
        json.registration = true;

        // 結果jsonの出力
        printJson(resp, YahoooooooUtils.Object2Json(json));

    }

    boolean validate(String id, String latittude, String longitude, String location) {
        if (id == null || latittude == null || longitude == null || location == null) {
            return false;
        }
        if (id.equals("") || latittude.equals("") || longitude.equals("")) {
            return false;
        }
        return true;
    }

    void printJson(HttpServletResponse resp, String outputJson) {
        resp.setContentType("text/plain");
        try {
            resp.getWriter().print(outputJson);
        } catch (IOException e) {
            e.printStackTrace();
            // FIXME:ログの出力
        }
    }
}
